<?php

namespace App\Kuafu\Export\Service\ExportFactory;

use App\Kuafu\Shop\Service\ShopDistributionService;

/**
 * app-渠道二维码下载
 */
class CreateDistributionQrcodeService extends BaseExportService
{
    private string $localPath = '';
    private string $tempPath  = '';

    public function __destruct()
    {
        // 清理临时文件
        if ($this->tempPath) {
            $fileSystem = getFileSystem();
            if ($fileSystem->isDirectory($this->tempPath)) {
                $fileSystem->deleteDirectory($this->tempPath);
            }
        }
    }

    public function query(): self
    {
        $shopId         = (int)($this->params['shopId'] ?? 0);
        $distributionId = (int)($this->params['distributionId'] ?? 0);
        $userId         = (int)($this->params['userId'] ?? 0);
        $needCode       = (bool)($this->params['needCode'] ?? false);
        $sourceType     = (int)($this->params['sourceType'] ?? 1);

        $res             = ShopDistributionService::genDistributionOrderCodeImg($shopId, $distributionId, $userId,
            $needCode, $sourceType);
        $this->localPath = $res['upright'] ?? "";//正面图
        $this->tempPath  = $res['templateBasePath'] ?? '';//返回文件存储临时目录

        return $this;
    }

    public function path(): string
    {
        return $this->upload($this->localPath);
    }
}
